using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Tickets;

namespace AndyPike.Unfuddle.Commands.Tickets
{
    /// <summary>
    /// Provides a command for updating an existing ticket on the Unfuddle server
    /// </summary>
    public class UpdateTicket : IUnfuddleChangeCommand
    {
        private readonly Ticket ticket;

        /// <summary>
        /// Instantiates the UpdateTicket command
        /// </summary>
        /// <param name="ticket">The existing ticket to update</param>
        public UpdateTicket(Ticket ticket)
        {
            this.ticket = ticket;
        }

        /// <summary>
        /// Updates an existing ticket on the Unfuddle server
        /// </summary>
        /// <param name="service">The Unfuddle service to use when executing the command</param>
        public void Execute(IUnfuddleService service)
        {
            GuardAgainst.ArgumentNull(service, "service");

            ticket.Validate(EntityValidationContext.Updating);

            string url = service.BuildUrl("projects/{0}/tickets/{1}", ticket.ProjectId, ticket.Id);
            service.InvokeRequest(url, HttpMethod.PUT, ticket);
        }
    }
}